import React from 'react';
import {Alert, Button, Card, DatePicker, Form, Input, InputNumber, Select} from "antd";
import {Link, useLocation} from "react-router-dom";
import {Part} from "type/Part";
import LogFactory from 'log/LogFactory';
import {OutboundDeliveryOrder} from "type/OutboundDeliveryOrder";
import {requestPut} from "util/request";
import ProForm from "common/form/ProForm";
import dayjs from "dayjs";
import ButtonGroup from "antd/es/button/button-group";
import OdoTypeSelect from "./OdoTypeSelect";
import {DEFAULT_DATE_TIME_FORMAT, MATHEMATICAL_PRECISION} from "constant/Common";
import {Dispatch} from "@reduxjs/toolkit";
import {updatePartInventory} from "../../../../redux/reducer/partReducer";
import {getLoginUserInfo} from "../../../../util/StorageUtil";

const log = LogFactory.getLog("PartOdoComponent");

const PartOdoComponent = () => {
    const part = {...(useLocation().state as Part)};
    const [form] = Form.useForm<OutboundDeliveryOrder>() ;

    const { username ,identity } = getLoginUserInfo() ;
    const initialValues: Partial<OutboundDeliveryOrder> = {
        partId: part.id,
        partName: part.partName,
        partFigureNumber: part.figureNumber,
        odoTime: dayjs(new Date()),
        odoOperator: username + " - " + identity ,
        odoSize: 1,
        odoPrice: 0.00
    } ;

    const onFinish = async (value: OutboundDeliveryOrder,dispatch: Dispatch) => {
        value.odoTime = value.odoTime.format(DEFAULT_DATE_TIME_FORMAT) ;
        value.odoPrice = value.odoPrice * MATHEMATICAL_PRECISION ;
        value.residualInventory = part.repertorySize - value.odoSize ;
        part.repertorySize = part.repertorySize - value.odoSize ;
        let res = await requestPut<number>("/calista/odo",value);
        log.debug("出库结果: ",res,dispatch) ;
        if (res.success){
            dispatch(updatePartInventory({partId: part.id,inventory: part.repertorySize})) ;
        }
        return res.success ;
    };
    const alert = <Alert message={"零件库存为空,无法进行出库操作!"} type={"warning"} showIcon style={{marginBottom: "10px"}}/> ;
    return (
        <Card title={"添加出库单"} extra={ <Link to={".."}>零件列表</Link> }>
            {
                part?.repertorySize === 0 ? alert : null
            }
            <ButtonGroup>
                <Button type={"primary"}>库存数量</Button>
                <Button type={"primary"}>{part?.repertorySize}</Button>
            </ButtonGroup>
            <ProForm<OutboundDeliveryOrder>
                initialValues={ initialValues }
                submit={onFinish}
                submitText={"添加出库单"}
                submitSuccessMessage={"添加出库单成功"}
                submitSuccessToUrl={".."}
                form={form}
                disabledSubmit={part?.repertorySize === 0}
            >
                <Form.Item<OutboundDeliveryOrder> label={"操作人"} name={"odoOperator"} required>
                    <Input disabled={true}/>
                </Form.Item>
                <Form.Item<OutboundDeliveryOrder> label={"零件数据编号"} name={"partId"} hidden required>
                    <Input disabled/>
                </Form.Item>
                <Form.Item<OutboundDeliveryOrder> label={"零件名称"} name={"partName"} required>
                    <Input disabled/>
                </Form.Item>
                <Form.Item<OutboundDeliveryOrder> label={"零件图号"} name={"partFigureNumber"} required>
                    <Input disabled/>
                </Form.Item>
                <OdoTypeSelect form={form}/>
                <Form.Item<OutboundDeliveryOrder> label={"零件编号"} name={"partSerialNumber"}>
                    <Input/>
                </Form.Item>
                <Form.Item<OutboundDeliveryOrder> label={"出库单描述"} name={"odoDesc"}>
                    <Input/>
                </Form.Item>
                <Form.Item<OutboundDeliveryOrder> label={"出库数量"} name={"odoSize"} required>
                    <InputNumber min={0} max={part?.repertorySize} addonAfter={"个"}/>
                </Form.Item>
                <Form.Item<OutboundDeliveryOrder> label="出库单价" name={"odoPrice"} required>
                    <InputNumber min={0.00} precision={2} addonAfter={"¥"}/>
                </Form.Item>
                <Form.Item<OutboundDeliveryOrder> label={"出库时间"} name={"odoTime"} required>
                    <DatePicker showTime/>
                </Form.Item>
            </ProForm>
        </Card>
    );
};

export default PartOdoComponent;